//
//  HomeViewController.m
//  xpkc
//
//  Created by TopSageOSX on 14/12/18.
//  Copyright (c) 2014年 wanmeizty. All rights reserved.
//

#import "HomeViewController.h"

@interface HomeViewController ()
{
    NSMutableArray *inArray;
    NSMutableArray *outArray;
    NSMutableArray *allArray;
}
@end

@implementation HomeViewController{
    OffLineViewController *OLVC;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.navigationItem.title = @"主页";
        self.tabBarItem.title = @"主页";
        self.tabBarItem.image = [UIImage imageNamed:@"listentest"];
        if (IsIOS7) {
            self.tabBarItem.selectedImage = [[UIImage imageNamed:@"listentest_selected"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        }
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.leftBarButtonItem = nil;
	[self getLocalHome];
}
#pragma mark -
#pragma mark == creatview ==

- (void)creatNavItem{
    UIImage *rightimg = [UIImage imageNamed:@"home_QR"];
    if (IsIOS7) {
        rightimg = [rightimg imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    }
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithImage:rightimg style:UIBarButtonItemStyleBordered target:self action:@selector(rightItemClick)];
    self.navigationItem.rightBarButtonItem = rightItem;
}
-(void)createHomeButton
{
    int count = self.view.subviews.count;
    for (int i = 0; i<count; i++) {
        [[self.view.subviews objectAtIndex:0]removeFromSuperview];
    }
	for (int i = 0 ; i < inArray.count; i++) {
		int x = i/3;
		int y = i%3;
		NSDictionary *btnDict = [inArray objectAtIndex:i];
        
        HomeButton *btn = [[HomeButton alloc]initWithFrame:CGRectMake(self.view.bounds.size.width/3.0*y, 64 + self.view.bounds.size.width/3.0*x, self.view.bounds.size.width/3.0-1, self.view.bounds.size.width/3.0-1)];
        btn.tag = i;
        [btn setValueWithDic:btnDict];
        [btn setStyle:HomeButtonStyleLight];
		[btn addTarget:self action:@selector(homeBtnClick:) forControlEvents:UIControlEventTouchUpInside];
		[self.view addSubview:btn];
	}
    //全局视图的创建
    if (OLVC == nil) {
        //容易多次创建
        OLVC = [[OffLineViewController alloc]init];
    }
}
#pragma mark -
#pragma mark == func ==
- (void)getLocalHome{
    allArray = [NSMutableArray array];
    inArray = [NSMutableArray array];
    outArray = [NSMutableArray array];
    inArray = [NSMutableArray arrayWithContentsOfFile:HOME_IN_PATH];
    outArray = [NSMutableArray arrayWithContentsOfFile:HOME_OUT_PATH];
    if (inArray.count == 0) {
        [self getAll];
    }
    [self createHomeButton];
}
- (void)getAll{
    NSArray *titleArray = @[@"公开课",@"备考资讯",@"选课中心",@"我的课程",@"个人中心",@"设置",@"离线管理",@"更多"];
    NSArray *typeArray = @[@"tab",@"push",@"push",@"tab",@"tab",@"push",@"push",@"more"];
    NSArray *enterindexArray = @[@"1",@"0",@"0",@"2",@"3",@"0",@"0",@"0"];
    NSArray *btnimgArray = @[@"home_opencourse",@"home_news",@"home_coursecenter",@"home_mycourse",@"home_user",@"home_set",@"home_offline",@"home_more"];
    NSArray *selectedimg = @[@"home_opencourse",@"home_news",@"home_coursecenter",@"home_mycourse",@"home_user",@"home_set",@"home_offline",@"home_more"];
    for (int i = 0; i<titleArray.count; i++) {
        NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:
                             [titleArray objectAtIndex:i],@"title",
                             [typeArray objectAtIndex:i],@"enterType",
                             [enterindexArray objectAtIndex:i],@"index",
                             [btnimgArray objectAtIndex:i],@"image",
                             [selectedimg objectAtIndex:i],@"selectImage",
                             nil];
        [allArray addObject:dic];
    }
    inArray = [NSMutableArray arrayWithArray:allArray];
    outArray = [NSMutableArray array];
}
- (void)saveLocalHome{
    logdebug(@"保存:in=%d--out=%d",
    [inArray writeToFile:HOME_IN_PATH atomically:YES],
    [outArray writeToFile:HOME_OUT_PATH atomically:YES]);
}
- (void)rightItemClick{
    logdebug(@"哦呵呵，右键按钮的");
}
-(void)homeBtnClick:(UIButton *)btn
{
	NSDictionary *btnDict = [inArray objectAtIndex:btn.tag];
    logdebug(@"%@",[btnDict objectForKey:@"title"]);
    
	if ([btnDict[@"enterType"] isEqualToString:@"tab"]) {
		int index = [btnDict[@"index"] intValue];
		self.tabBarController.selectedIndex = index;
	}
	else if ([btnDict[@"enterType"] isEqualToString:@"push"]) {
        if ([btnDict[@"title"] isEqualToString:@"选课中心"]) {
            //FIXME:判断方式有待改进
            CourseCenterViewController *CCVC = [[CourseCenterViewController alloc]init];
            [self.navigationController pushViewController:CCVC animated:YES];
        }
        if ([btnDict[@"title"] isEqualToString:@"备考资讯"]) {
            NewsViewController *vc = [[NewsViewController alloc]init];
            vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
        }
        if ([btnDict[@"title"] isEqualToString:@"离线管理"]) {
            
            OLVC.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:OLVC animated:YES];
        }
        if ([btnDict[@"title"] isEqualToString:@"设置"]) {
            UserViewController *VC = [[UserViewController alloc]init];
            [VC setHidesBottomBarWhenPushed:YES];
            [self.navigationController pushViewController:VC animated:YES];
        }
	}
	else if ([btnDict[@"enterType"] isEqualToString:@"present"]) {
		
	}
	else if ([btnDict[@"enterType"] isEqualToString:@"more"]) {
        HomeManageViewController *HMVC = [[HomeManageViewController alloc]init];
        HMVC.inArray = inArray;
        HMVC.outArray = outArray;
        //TODO:使用本地文件读写也可以，block返回参数也可以，毕竟进入初始化的时候是读取的本地文件，偏向于读取本地文件
        //block
        HMVC.homeblock = ^(NSMutableArray *inA,NSMutableArray *outA){
            inArray = [NSMutableArray arrayWithArray:inA];
            outArray = [NSMutableArray arrayWithArray:outA];
            [self createHomeButton];
            [self saveLocalHome];
        };
        [self.navigationController pushViewController:HMVC animated:YES];
	}
}

-(BOOL)shouldAutorotate
{
    return NO;
}

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}
#pragma mark -
#pragma mark == other ==
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
